package org.team751.sensors;

import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.SensorBase;
import edu.wpi.first.wpilibj.parsing.ISensor;

/**
 * Manages one of those thermistors. One of those thermistors is a thermistor in the shape of a black disk with the number 503 printed on it.
 * It should be wired like this:
 * <pre>
 * [analog signal line]        [+5v line]         [ground line]
 *         |                        |                    |
 *         |---[10K ohm resistor]---|                    |
 *         |                                             |
 *         |-------------[Thermistor]--------------------|
 * </pre>
 * @author Sam Crow
 */
public class Thermistor extends SensorBase implements ISensor {
    private AnalogChannel channel;

    private static final double kScaleFactor = -21.45045965270684;
    private static final double kOffset = 117.3513789581205;

    /**
     * Constructor with a given module and channel
     * @param slot The cRIO module slot
     * @param channel The analog channel in that module
     */
    public Thermistor(int slot, int channel){
        this.channel = new AnalogChannel(slot, channel);
        init();
    }
    /**
     * Constructor with the default cRIO module slot
     * @param channel The analog channel in the default analog module
     */
    public Thermistor(int channel){
        this.channel = new AnalogChannel(channel);
        init();
    }

    /**
     * Configure the analog channel thing as desired
     */
    private void init(){
        channel.setAverageBits(4);
    }

    /**
     * Get the temperature currently being measured by the thermistor
     * @return The temperature in degrees celsius
     */
    public double getTemperature(){
        /*
         * Temperature table:
         * 32 C = 3.979 volts
         * 11 C = 4.498 volts
         * Too hot for a CIM motor = ? volts
         * graphing with voltage on X axis, temperature on Y axis:
         * function: C(v) = mv+b
         * m = -21.45045965270684
         * b = 117.3513789581205
         */
        double volts = channel.getAverageVoltage();

        return volts * kScaleFactor + kOffset;
    }
}
